package org.aj.web.exception;

import org.aj.web.exception.prop.AlarmProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jcj
 * @description用于配置属性
 * date  2021/4/11 21:42
 */
@ConfigurationProperties(prefix = "aj.web.exception")
public class CustomProperties {


    /**
     * 服务器器错误 即500时候的友好提示语
     */
   private String custom500Msg = "系统繁忙";


    /**
     *springboot项目加载哪些配置下才用这个默认提示语 默认为生产环境即prod
     * 是否当服务发生异常的时候是否启用自定义提示语返回 还是直接返回整个错误堆栈信息
     * 开发 调试阶段可以关闭这样可以快速看到错误信息而不用看日志信息
     * 生产环境建议保持默认
     */
    private boolean enableCustomMsgWhenServerError = true;

    /**
     * 接收邮件的地址
     */
    private String[] receiveMailAddr;

    /**
     * 发送方的邮件地址
     */
    private String sendMailAddr;

    /**
     * 邮件标题
     */
    private String mailTitle = "接口异常";

    /**
     * 用于控制异常处理器在springmvc 的位置，若对mvc的异常处理机制不了解 可以不设置
     */
    private Integer order;


    /**
     * 请求参数对应的key 即 会以此作为key 从request里面取出请求参数 如果有的话
     * 通过 request.getAttribute 若取不到则从 request.getHeader 尝试获取
     */
    private String reqParamsKey = "REQUEST_PARAMS";

    /**
     * 对请求头的参数值进行解码(URLDecode)的时候 使用什么字符集 默认为utf-8
     *
     */
    private String decodeReqHeadCharset="UTF-8";
    /**
     * 配置从请求头的哪些key 或者？请求参数里面哪些key 获取授权信息
     * 默认为 Authorization key
     */
    private String[] authorizationKey = new String[]{"Authorization"};

    /**
     * 云节点标识对应的key ,插件会以此作为属性名称从系统环境变量
     * 读取，若读取不到则从spring上下文里面获取用来标识邮件是从
     * 哪个节点发出 若还是获取不到 则尝试通过调用java api来获取
     * 运行的ip地址 通过web监听器去获取端口号
     */
    private String cloudNodeId="eureka.instance.instance-id";


    /**
     * 单个uri连续发送邮件最大次数，默认为5
     */
    private Integer sendMailMaxCount=5;


    /**
     * 间隔多久进行第二轮邮件发送
     */
    private Duration sendMailInterval = Duration.ofHours(2);

    /**
     * 使用异步发送邮件的线程池的名称 须是java.util.concurrent.Executor 之类
     */
    private String sendMailUseAsyThreadPoolExecutorBeanName;

    /**
     * 用来从请求头获取请求属性里面取出请求时间的对于的key值名称
     */
    private String requestStartTimeKey="REQUEST_START_TIME";

    /**
     * 不希望这个插件拦截的异常
     * 注意
     * {@link org.aj.base.exception.BusException}
     * 传入这个是无效的，传入这个证明你用的就是整套体系
     */
    private List<String> excludeExceptionList = new ArrayList<>();


    /**
     * 当 Exception.getMessage() 返回空的时候从 异常的堆栈信息去多长作为提示语
     */
    private Integer truncateStackTraceLength=100;


    /**
     * 有时候不希望直接获取Exception.getMessage()或者堆栈里面的错误信息，而是直接使用配置的默认错误提示语{@link CustomProperties#getCustom500Msg()}
     * 这里记录的是类的全路径 例如空指针异常：{@link NullPointerException} java.lang.NullPointerException
     */
    private List<String> useCustom500MsgExceptionList = new ArrayList<>();

    /**
     * 尝试获取异常导致的cause即{@link Exception#getCause()}的深度，0表示不获取
     */
    private int tryGetExceptionCauseDepth=20;

    @NestedConfigurationProperty
    private AlarmProperties alarmProperties;

    public String getSendMailUseAsyThreadPoolExecutorBeanName() {
        return sendMailUseAsyThreadPoolExecutorBeanName;
    }

    public void setSendMailUseAsyThreadPoolExecutorBeanName(String sendMailUseAsyThreadPoolExecutorBeanName) {
        this.sendMailUseAsyThreadPoolExecutorBeanName = sendMailUseAsyThreadPoolExecutorBeanName;
    }



    public String getDecodeReqHeadCharset() {
        return decodeReqHeadCharset;
    }

    public CustomProperties setDecodeReqHeadCharset(String decodeReqHeadCharset) {
        this.decodeReqHeadCharset = decodeReqHeadCharset;
        return this;
    }

    public Integer getOrder() {
        return order;
    }

    public CustomProperties setOrder(Integer order) {
        this.order = order;
        return this;
    }

    public String getReqParamsKey() {
        return reqParamsKey;
    }

    public CustomProperties setReqParamsKey(String reqParamsKey) {
        this.reqParamsKey = reqParamsKey;
        return this;
    }

    public String getMailTitle() {
        return mailTitle;
    }

    public CustomProperties setMailTitle(String mailTitle) {
        this.mailTitle = mailTitle;
        return this;
    }

    public String getSendMailAddr() {
        return sendMailAddr;
    }

    public CustomProperties setSendMailAddr(String sendMailAddr) {
        this.sendMailAddr = sendMailAddr;
        return this;
    }

    public String[] getReceiveMailAddr() {
        return receiveMailAddr;
    }

    public CustomProperties setReceiveMailAddr(String[] receiveMailAddr) {
        this.receiveMailAddr = receiveMailAddr;
        return this;
    }

    public String getCustom500Msg() {
        return custom500Msg;
    }

    public CustomProperties setCustom500Msg(String custom500Msg) {
        this.custom500Msg = custom500Msg;
        return this;
    }

    public boolean isEnableCustomMsgWhenServerError() {
        return enableCustomMsgWhenServerError;
    }

    public CustomProperties setEnableCustomMsgWhenServerError(boolean enableCustomMsgWhenServerError) {
        this.enableCustomMsgWhenServerError = enableCustomMsgWhenServerError;
        return this;
    }

    public String getCloudNodeId() {
        return cloudNodeId;
    }

    public void setCloudNodeId(String cloudNodeId) {
        this.cloudNodeId = cloudNodeId;
    }

    public Integer getSendMailMaxCount() {
        return sendMailMaxCount;
    }

    public void setSendMailMaxCount(Integer sendMailMaxCount) {
        this.sendMailMaxCount = sendMailMaxCount;
    }

    public Duration getSendMailInterval() {
        return sendMailInterval;
    }

    public void setSendMailInterval(Duration sendMailInterval) {
        this.sendMailInterval = sendMailInterval;
    }

    public String getRequestStartTimeKey() {
        return requestStartTimeKey;
    }

    public void setRequestStartTimeKey(String requestStartTimeKey) {
        this.requestStartTimeKey = requestStartTimeKey;
    }

    public List<String> getExcludeExceptionList() {
        return excludeExceptionList;
    }

    public void setExcludeExceptionList(List<String> excludeExceptionList) {
        this.excludeExceptionList = excludeExceptionList;
    }


    public Integer getTruncateStackTraceLength() {
        return truncateStackTraceLength;
    }

    public void setTruncateStackTraceLength(Integer truncateStackTraceLength) {
        this.truncateStackTraceLength = truncateStackTraceLength;
    }

    public List<String> getUseCustom500MsgExceptionList() {
        return useCustom500MsgExceptionList;
    }

    public void setUseCustom500MsgExceptionList(List<String> useCustom500MsgExceptionList) {
        this.useCustom500MsgExceptionList = useCustom500MsgExceptionList;
    }

    public int getTryGetExceptionCauseDepth() {
        return tryGetExceptionCauseDepth;
    }

    public void setTryGetExceptionCauseDepth(int tryGetExceptionCauseDepth) {
        this.tryGetExceptionCauseDepth = tryGetExceptionCauseDepth;
    }

    public String[] getAuthorizationKey() {
        return authorizationKey;
    }

    public void setAuthorizationKey(String[] authorizationKey) {
        this.authorizationKey = authorizationKey;
    }

    public AlarmProperties getAlarmProperties() {
        return alarmProperties;
    }

    public void setAlarmProperties(AlarmProperties alarmProperties) {
        this.alarmProperties = alarmProperties;
    }
}